//
//  CYGLeftMenuViewController.m
//  CYGHuntForCity
//
//  Created by cygarm on 16/2/21.
//  Copyright © 2016年 cygarm. All rights reserved.
//

#import "CYGLeftMenuViewController.h"
#import "CYGHomeViewController.h"
#import "CYGFoundViewController.h"
#import "CYGRightViewController.h"

@interface CYGLeftMenuViewController () <CYGLeftMenuCityViewDelegate>
@property (weak, nonatomic) IBOutlet UIButton *cityBtn;
@property (weak, nonatomic) IBOutlet UIButton *homeBtn;
@property (weak, nonatomic) IBOutlet UIButton *foundBtn;
@property (weak, nonatomic) IBOutlet UIButton *sinaLogin;
@property (weak, nonatomic) IBOutlet UIButton *weixinLogin;
@property (weak, nonatomic) IBOutlet UIButton *msgBtn;
@property (weak, nonatomic) IBOutlet UIButton *settingBtn;

@property (strong, nonatomic) NSMutableArray<NSString*> *cities;
@property (strong, nonatomic) CYGLeftMenuCityView *cityView;
@property (strong, nonatomic) UIView *cityBackView;

@property (strong, nonatomic) CYGRightViewController *rightVC;
@end

@implementation CYGLeftMenuViewController

- (void)viewDidLoad {
    [super viewDidLoad];
	
	[self setUpViewControllerBtn];
	_cities = [NSMutableArray arrayWithArray:@[@"广州", @"深圳", @"北京", @"上海"]];
	[self setUpCityBtn];
	
}

- (void)viewDidAppear:(BOOL)animated {
	static dispatch_once_t onceToken;
	dispatch_once(&onceToken, ^{
		
		[self.homeBtn sendActionsForControlEvents:UIControlEventTouchUpInside];
	});
}

- (void)viewDidLayoutSubviews {

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UI setup

- (void) setUpViewControllerBtn {
	[self.homeBtn addTarget:self action:@selector(RightViewBtnClick:) forControlEvents:UIControlEventTouchUpInside];
	self.homeBtn.titleLabel.text = kidVCHome;
	[self.foundBtn addTarget:self action:@selector(RightViewBtnClick:) forControlEvents:UIControlEventTouchUpInside];
	self.foundBtn.titleLabel.text = kidVCFound;
}

- (void)setUpCityBtn {
	self.cityBtn.layer.masksToBounds = YES;
	self.cityBtn.layer.cornerRadius = CYGCornerRadius;
	[self.cityBtn setTitle:_cities[0] forState:UIControlStateNormal];
	[self.cityBtn setTitleColor:CYGGolbalGreen forState:UIControlStateNormal];
	[self.cityBtn setImage:[UIImage imageNamed:@"city_down"] forState:UIControlStateNormal];
	[self.cityBtn setImage:[UIImage imageNamed:@"city_up"] forState:UIControlStateSelected];
	[self.cityBtn.superview bringSubviewToFront:self.cityBtn];
}

- (void)setUpCityBackView {
	CGFloat frameX = self.cityBtn.frame.origin.x;
	CGFloat frameY = self.cityBtn.frame.origin.y;
	CGFloat btnW = self.cityBtn.bounds.size.width;
	CGFloat btnH = self.cityBtn.bounds.size.height;
	self.cityBackView = [[UIView alloc] initWithFrame:CGRectMake(frameX, frameY, btnW, btnH*4)];
	self.cityBackView.backgroundColor = [UIColor blackColor];
	self.cityBackView.layer.masksToBounds = YES;
	self.cityBackView.layer.cornerRadius = CYGCornerRadius;
	self.cityBackView.alpha = 0;
	self.cityView = [[CYGLeftMenuCityView alloc] initWithFrame:CGRectZero];
	self.cityView.frame = CGRectMake(0, btnH, btnW, btnH*3);
	[self.cityView initCities:_cities];
	self.cityView.delegate = self;
	[self.cityBackView addSubview:self.cityView];
	[self.cityBtn.superview insertSubview:self.cityBackView belowSubview:self.cityBtn];
}

#pragma mark - Navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
	DLog(@"%@",segue.description);
//	if ([segue.identifier isEqualToString:@"idSegueShowHome"]) {
//		_homeNav = segue.destinationViewController;
//	} else if ([segue.identifier isEqualToString:@"idSegueShowFound"]) {
//		_foundNav = segue.destinationViewController;
//	}
}

- (IBAction)returnFromLeftMenuNaviSegue:(UIStoryboardSegue *)segue {

}

- (void)resegueBtnClick:(UIButton *)sender {
	DLog();
	NSString *unsegueId = sender.titleLabel.text;
	
	if ([unsegueId isEqualToString:@"seugeLeftMenuShowNaviUnwindHome"]) {
		[self.homeBtn sendActionsForControlEvents:UIControlEventTouchUpInside];
	} else if ([unsegueId isEqualToString:@"seugeLeftMenuShowNaviUnwindFound"]) {
		[self.foundBtn sendActionsForControlEvents:UIControlEventTouchUpInside];
	}
}

#pragma mark - CYGLeftMenuCityViewDelegate
- (void)leftMenuCityViewBtnClicked:(NSInteger)btnIndex {
	[_cities exchangeObjectAtIndex:0 withObjectAtIndex:btnIndex];
	[self.cityBtn setTitle:_cities[0] forState:UIControlStateNormal];
	[self CityBtnClick:self.cityBtn];
}

#pragma mark - IBAction
- (IBAction)CityBtnClick:(UIButton*)sender {
	sender.selected = !sender.selected;
	if (sender.selected) {
		[self setUpCityBackView];
		[UIView animateWithDuration:0.3 animations:^{
			self.cityBackView.alpha = 0.75;
		}];
	}
	else {
		[UIView animateWithDuration:0.3 animations:^{
			self.cityBackView.alpha = 0;
		} completion:^(BOOL finished) {
			[self.cityBackView removeFromSuperview];
		}];
	}
}

- (void)RightViewBtnClick:(UIButton *)sender {
	if (![_rightVC.restorationIdentifier isEqualToString:sender.titleLabel.text]) {
		
		if (_rightVC != nil) {
			[_rightVC.navigationController.view removeFromSuperview];
			[_rightVC.navigationController removeFromParentViewController];
			_rightVC = nil;
		}

		_rightVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:sender.titleLabel.text];
		UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:_rightVC];
		[self addChildViewController:nav];
		[self.view addSubview:nav.view];
		nav.view.frame = self.view.frame;
		[_rightVC scaleToRight];
	}
	
	[_rightVC moveToFull];
}

@end
